<?php

class SettingHelper extends Helper {

    private $_auditfield;
    private $audit_rule;

    public function __construct() {
        $this->_auditfield = array('au_id', 'au_code', 'au_level', 'au_ceiling', 'au_remark');
        $this->audit_rule = array(
            'au_code' => array('require', '权限编号不能为空'),
            //'au_level' => array('require', '审核级别不能为空'),
            'au_ceiling' => array('number', '审核上限值错误')
        );
        
    }
    /**
     * 
     * @return type
     */
    public function auditSave($data, $where = ''){
        if(is_numeric($where)){
            $where = array('`au_id` = ?' => $where);
        }
        $validator = Validator::checkRule($data, $this->audit_rule);
        if(!$validator['pass']){
            return array('success' => false, 'msg' => $validator['msg']);
        }
        $auditModel = $this->LoadModel('Public'); 
        if(empty($where)){
            $id = $auditModel->save('setting_audit', $data);
            if($id){
                return array('success' => true, 'msg' => $id);
            }else{
                return array('success' => false, 'msg' => '保存失败');
            }
        }else{
            $auditModel->update('setting_audit', $data, $where);
            return array('success' => true);
        }
    }
    /**
     * 
     * @param type $where
     * @param type $page
     * @param type $limit
     * @param type $param
     * @return type
     */
    public function auditList($where, $page = 1, $limit = PAGE_SIZE, $param = array()){
        $auditModel = $this->LoadModel('Public'); 
        $data['count'] = $auditModel->getRowCount('setting_audit', $where);
        if(!empty($data['count'])){
            $data['all'] = $auditModel->getList('setting_audit', $this->_auditfield, $where, array($page, $limit), NULL, array('au_id DESC'));
            Pagination::SetUrl($Param);
            $data['page'] = Pagination::GetHtml($limit, $page, $data['count']);
        }
        return $data;
    }
    
    public function auditAll($where){
        if(is_numeric($where)){
            $where = array('`au_id` = ?' => $where);
        }
        $auditModel = $this->LoadModel('Public'); 
        $data = $auditModel->getList('setting_audit', $this->_auditfield, $where);
        return $data;
    }
    
    public function auditRow($where){
        if(is_numeric($where)){
            $where = array('`au_id` = ?' => $where);
        }
        $auditModel = $this->LoadModel('Public'); 
        $data = $auditModel->getOneRow('setting_audit', $this->_auditfield, $where);
        return $data;
    }
    
    public function auditRemove($where){
        if(is_numeric($where)){
            $where = array('`au_id` = ?' => $where);
        }
        $auditModel = $this->LoadModel('Public'); 
        $do = $auditModel->update('setting_audit', array('au_status' => 'del'), $where);
        return $do;
    }
    
    public function auditLevel($au_id){
        $last_level = array();
        $my_level = array();
        $model = $this->LoadModel('Public');
        $all_level = $model->getList('setting_audit', $this->_auditfield, array('`au_status` = ?' => 'ok'), null, null, array('au_ceiling ASC'));
        if(!empty($all_level)) foreach($all_level as $k => $v){
            if($v['au_id'] == $au_id){
                 $my_level = $v ;
                 break;
            }
            $last_level = $v;
        }
        return array(
            'last' => $last_level,
            'my' => $my_level
        );
        
    }
    
}